package com.jhgsys.internal.pm.service.impl;

import com.jhgsys.internal.pm.entity.DemoBook;
import com.jhgsys.internal.pm.mapper.DemoBookMapper;
import com.jhgsys.internal.pm.service.IDemoBookService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 示例-图书 Service实现
 *
 * @author chenweihua
 * @date 2022-02-05 11:07:44
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DemoBookServiceImpl extends ServiceImpl<DemoBookMapper, DemoBook> implements IDemoBookService {

    /**
     * 查询（分页）
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     * @param request QueryRequest
     * @param demoBook demoBook
     * @return IPage<DemoBook>
    */
    @Override
    public IPage<DemoBook> findDemoBooks(QueryRequest request, DemoBook demoBook) {
        LambdaQueryWrapper<DemoBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DemoBook::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<DemoBook> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     * @param demoBook demoBook
     * @return List<DemoBook>
     */
    @Override
    public List<DemoBook> findDemoBooks(DemoBook demoBook) {
	    LambdaQueryWrapper<DemoBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DemoBook::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author chenweihua
    * @date 2022-02-05 11:07:44
    * @param id
    * @return DemoBook
    */
    @Override
    public DemoBook findDemoBookById(Integer id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     * @param demoBook demoBook
     */
    @Override
    @Transactional
    public void createDemoBook(DemoBook demoBook) {
        demoBook.setCreatedTime(new Date());
        this.save(demoBook);
    }

    /**
     * 修改
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     * @param demoBook demoBook
     */
    @Override
    @Transactional
    public void updateDemoBook(DemoBook demoBook) {
        demoBook.setUpdatedTime(new Date());
        this.saveOrUpdate(demoBook);
    }

    /**
     * 逻辑删除
     * @author chenweihua
     * @date 2022-02-05 11:07:44
     * @param demoBook demoBook
     * @param demoBookIds demoBookIds
     */
    @Override
    @Transactional
    public void deleteDemoBook(DemoBook demoBook,String demoBookIds) {
        List<String> list = Arrays.asList(demoBookIds.split(StringPool.COMMA));
        this.baseMapper.update(demoBook,new LambdaQueryWrapper<DemoBook>().in(DemoBook::getId,list));
	}

    /**
    * 物理删除
    * @author chenweihua
    * @date 2022-02-05 11:07:44
    * @param demoBookIds demoBookIds
    */
    @Override
    @Transactional
    public void physicallyDeleteDemoBook(String demoBookIds) {
        List<String> list = Arrays.asList(demoBookIds.split(StringPool.COMMA));
        LambdaQueryWrapper<DemoBook> wapper = new LambdaQueryWrapper<>();
        wapper.in(DemoBook::getId,list);
        this.remove(wapper);
    }
}
